summaryrefslogtreecommitdiff
path: root/vendor/github.com/playwright-community/playwright-go/channel_owner.go
blob: 5159eb2c23c8d4b850ade9ab019683070d005f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package playwright

import (
	"sync"
)

type channelOwner struct {
	sync.RWMutex
	eventEmitter
	objectType                 string
	guid                       string
	channel                    *channel
	objects                    map[string]*channelOwner
	eventToSubscriptionMapping map[string]string
	connection                 *connection
	initializer                map[string]interface{}
	parent                     *channelOwner
	wasCollected               bool
	isInternalType             bool
}

func (c *channelOwner) dispose(reason ...string) {
	// Clean up from parent and connection.
	if c.parent != nil {
		delete(c.parent.objects, c.guid)
	}
	c.connection.objects.Delete(c.guid)
	if len(reason) > 0 {
		c.wasCollected = reason[0] == "gc"
	}

	// Dispose all children.
	for _, object := range c.objects {
		object.dispose(reason...)
	}
	c.objects = make(map[string]*channelOwner)
}

func (c *channelOwner) adopt(child *channelOwner) {
	delete(child.parent.objects, child.guid)
	c.objects[child.guid] = child
	child.parent = c
}

func (c *channelOwner) setEventSubscriptionMapping(mapping map[string]string) {
	c.eventToSubscriptionMapping = mapping
}

func (c *channelOwner) updateSubscription(event string, enabled bool) {
	protocolEvent, ok := c.eventToSubscriptionMapping[event]
	if ok {
		c.channel.SendNoReplyInternal("updateSubscription", map[string]interface{}{
			"event":   protocolEvent,
			"enabled": enabled,
		})
	}
}

func (c *channelOwner) Once(name string, handler interface{}) {
	c.addEvent(name, handler, true)
}

func (c *channelOwner) On(name string, handler interface{}) {
	c.addEvent(name, handler, false)
}

func (c *channelOwner) addEvent(name string, handler interface{}, once bool) {
	if c.ListenerCount(name) == 0 {
		c.updateSubscription(name, true)
	}
	c.eventEmitter.addEvent(name, handler, once)
}

func (c *channelOwner) RemoveListener(name string, handler interface{}) {
	c.eventEmitter.RemoveListener(name, handler)
	if c.ListenerCount(name) == 0 {
		c.updateSubscription(name, false)
	}
}

func (c *channelOwner) createChannelOwner(self interface{}, parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) {
	c.objectType = objectType
	c.guid = guid
	c.wasCollected = false
	c.parent = parent
	c.objects = make(map[string]*channelOwner)
	c.initializer = initializer
	if c.parent != nil {
		c.connection = parent.connection
		c.parent.objects[guid] = c
	}
	if c.connection != nil {
		c.connection.objects.Store(guid, c)
	}
	c.channel = newChannel(c, self)
	c.eventToSubscriptionMapping = map[string]string{}
}

func (c *channelOwner) markAsInternalType() {
	c.isInternalType = true
}

type rootChannelOwner struct {
	channelOwner
}

func (r *rootChannelOwner) initialize() (*Playwright, error) {
	ret, err := r.channel.SendReturnAsDict("initialize", map[string]interface{}{
		"sdkLanguage": "javascript",
	})
	if err != nil {
		return nil, err
	}
	return fromChannel(ret["playwright"]).(*Playwright), nil
}

func newRootChannelOwner(connection *connection) *rootChannelOwner {
	c := &rootChannelOwner{}
	c.connection = connection
	c.createChannelOwner(c, nil, "Root", "", make(map[string]interface{}))
	return c
}