@player-ui/react-subscribe 0.8.0--canary.307.9645 → 0.8.0--canary.410.15865

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/subscribe/src/index.tsx
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Subscribe: () => Subscribe,
34
+ useSubscribedState: () => useSubscribedState
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+ var import_react = __toESM(require("react"));
38
+ function deferred() {
39
+ let resolve = () => void 0;
40
+ let reject = () => void 0;
41
+ let status = "pending";
42
+ const promise = new Promise((res, rej) => {
43
+ resolve = (a) => {
44
+ status = "success";
45
+ const resolveFunc = res;
46
+ resolveFunc(a);
47
+ };
48
+ reject = (error) => {
49
+ status = "failure";
50
+ rej(error);
51
+ };
52
+ });
53
+ return {
54
+ resolve,
55
+ status,
56
+ reject,
57
+ promise
58
+ };
59
+ }
60
+ var NOT_CALLED = Symbol("Subscribe -- Empty Value");
61
+ var Subscribe = class {
62
+ constructor() {
63
+ this.callbacks = /* @__PURE__ */ new Map();
64
+ this.deferredResult = deferred();
65
+ this.lastValue = NOT_CALLED;
66
+ this.resetDeferred = null;
67
+ this.publish = this.publish.bind(this);
68
+ this.add = this.add.bind(this);
69
+ this.remove = this.remove.bind(this);
70
+ }
71
+ /**
72
+ * Trigger the subscriptions using the provided value
73
+ * if there is a reset in progress, wait for it before publishing a new value.
74
+ */
75
+ async publish(val) {
76
+ await this.resetDeferred?.promise;
77
+ this.lastValue = val;
78
+ this.deferredResult.resolve(val);
79
+ this.callbacks.forEach((c) => c(val));
80
+ }
81
+ /**
82
+ * Subscribe to updates
83
+ */
84
+ add(callback, options) {
85
+ const id = this.callbacks.size;
86
+ this.callbacks.set(id, callback);
87
+ if (this.lastValue !== NOT_CALLED && options?.initializeWithPreviousValue === true) {
88
+ callback(this.lastValue);
89
+ }
90
+ return id;
91
+ }
92
+ /**
93
+ * Remove any updates from the given listener
94
+ */
95
+ remove(id) {
96
+ this.callbacks.delete(id);
97
+ }
98
+ /**
99
+ * Reset the state of the listener
100
+ * Passing in a promise will defer resetting the view until the promise is resolved
101
+ */
102
+ async reset(promise) {
103
+ if (promise) {
104
+ this.resetDeferred = deferred();
105
+ await promise;
106
+ }
107
+ if (this.lastValue !== NOT_CALLED) {
108
+ this.deferredResult = deferred();
109
+ }
110
+ this.lastValue = NOT_CALLED;
111
+ this.callbacks.forEach((c) => c(void 0));
112
+ this.resetDeferred?.resolve();
113
+ this.resetDeferred = null;
114
+ }
115
+ /**
116
+ * _Throws_ a promise if the value is still pending
117
+ * Otherwise returns it
118
+ */
119
+ suspend() {
120
+ if (this.lastValue === NOT_CALLED) {
121
+ throw this.deferredResult.promise;
122
+ }
123
+ return this.lastValue;
124
+ }
125
+ /** Get the current value of the subscription */
126
+ get() {
127
+ if (this.lastValue === NOT_CALLED) {
128
+ return void 0;
129
+ }
130
+ return this.lastValue;
131
+ }
132
+ };
133
+ function useSubscribedState(subscriber) {
134
+ const [state, setState] = import_react.default.useState(subscriber.get());
135
+ import_react.default.useEffect(() => {
136
+ const id = subscriber.add(
137
+ (resp) => {
138
+ setState(resp);
139
+ },
140
+ {
141
+ initializeWithPreviousValue: true
142
+ }
143
+ );
144
+ return () => {
145
+ subscriber.remove(id);
146
+ };
147
+ }, [subscriber]);
148
+ return state;
149
+ }
150
+ // Annotate the CommonJS export names for ESM import in node:
151
+ 0 && (module.exports = {
152
+ Subscribe,
153
+ useSubscribedState
154
+ });
155
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/subscribe/src/index.tsx"],"sourcesContent":["import React from \"react\";\n\nexport type SubscribeID = number;\n\ntype ResolveType<T> = (arg?: T) => void;\ntype RejectType = (error?: Error) => void;\ntype StatusType = \"success\" | \"failure\" | \"pending\";\ntype DefferedReturnType<T> = {\n /** a function to resolve the promise */\n resolve: ResolveType<T>;\n\n /** a function to reject the promise */\n reject: RejectType;\n\n /** the status of the promise */\n status: StatusType;\n\n /** a promise to express the above */\n promise: Promise<T>;\n};\n\n/** create a deferred promise */\nfunction deferred<T>(): DefferedReturnType<T> {\n /** the default resolve handler is a noop */\n let resolve: ResolveType<T> = () => undefined;\n\n /** the default reject handler is a noop */\n let reject: RejectType = () => undefined;\n\n let status: StatusType = \"pending\";\n\n const promise = new Promise<T>((res, rej) => {\n resolve = (a?: T) => {\n status = \"success\";\n const resolveFunc = res as ResolveType<T>;\n resolveFunc(a);\n };\n\n reject = (error?: Error) => {\n status = \"failure\";\n rej(error);\n };\n });\n\n return {\n resolve,\n status,\n reject,\n promise,\n };\n}\n\nconst NOT_CALLED = Symbol(\"Subscribe -- Empty Value\");\n/**\n * A pub-sub module that works across the React bridge\n */\nexport class Subscribe<T> {\n private callbacks: Map<SubscribeID, (val: T | undefined) => void> = new Map();\n private deferredResult = deferred<T>();\n private lastValue: T | typeof NOT_CALLED = NOT_CALLED;\n private resetDeferred: DefferedReturnType<void> | null = null;\n constructor() {\n this.publish = this.publish.bind(this);\n this.add = this.add.bind(this);\n this.remove = this.remove.bind(this);\n }\n\n /**\n * Trigger the subscriptions using the provided value\n * if there is a reset in progress, wait for it before publishing a new value.\n */\n async publish(val: T): Promise<void> {\n await this.resetDeferred?.promise;\n this.lastValue = val;\n this.deferredResult.resolve(val);\n this.callbacks.forEach((c) => c(val));\n }\n\n /**\n * Subscribe to updates\n */\n add(\n callback: (arg: T | undefined) => void,\n options?: {\n /** Use the last updated value for this subscription to immediately trigger the onSet callback */\n initializeWithPreviousValue?: boolean;\n },\n ): SubscribeID {\n const id = this.callbacks.size;\n this.callbacks.set(id, callback);\n\n if (\n this.lastValue !== NOT_CALLED &&\n options?.initializeWithPreviousValue === true\n ) {\n callback(this.lastValue);\n }\n\n return id;\n }\n\n /**\n * Remove any updates from the given listener\n */\n remove(id: SubscribeID) {\n this.callbacks.delete(id);\n }\n\n /**\n * Reset the state of the listener\n * Passing in a promise will defer resetting the view until the promise is resolved\n */\n async reset(promise?: Promise<void>) {\n if (promise) {\n this.resetDeferred = deferred<void>();\n await promise;\n }\n\n if (this.lastValue !== NOT_CALLED) {\n this.deferredResult = deferred();\n }\n\n this.lastValue = NOT_CALLED;\n this.callbacks.forEach((c) => c(undefined));\n\n this.resetDeferred?.resolve();\n this.resetDeferred = null;\n }\n\n /**\n * _Throws_ a promise if the value is still pending\n * Otherwise returns it\n */\n suspend(): T {\n if (this.lastValue === NOT_CALLED) {\n throw this.deferredResult.promise;\n }\n\n return this.lastValue;\n }\n\n /** Get the current value of the subscription */\n get(): T | undefined {\n if (this.lastValue === NOT_CALLED) {\n return undefined;\n }\n\n return this.lastValue;\n }\n}\n\nexport interface SubscribedStateHookOptions {\n /** if the state should trigger suspense when waiting to resolve */\n suspend?: boolean;\n}\n\n/** Subscribe to a state change event in a react component */\nexport function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined {\n const [state, setState] = React.useState<T | undefined>(subscriber.get());\n\n React.useEffect(() => {\n const id = subscriber.add(\n (resp) => {\n setState(resp);\n },\n {\n initializeWithPreviousValue: true,\n },\n );\n\n return () => {\n subscriber.remove(id);\n };\n }, [subscriber]);\n\n return state;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAsBlB,SAAS,WAAqC;AAE5C,MAAI,UAA0B,MAAM;AAGpC,MAAI,SAAqB,MAAM;AAE/B,MAAI,SAAqB;AAEzB,QAAM,UAAU,IAAI,QAAW,CAAC,KAAK,QAAQ;AAC3C,cAAU,CAAC,MAAU;AACnB,eAAS;AACT,YAAM,cAAc;AACpB,kBAAY,CAAC;AAAA,IACf;AAEA,aAAS,CAAC,UAAkB;AAC1B,eAAS;AACT,UAAI,KAAK;AAAA,IACX;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,IAAM,aAAa,OAAO,0BAA0B;AAI7C,IAAM,YAAN,MAAmB;AAAA,EAKxB,cAAc;AAJd,SAAQ,YAA4D,oBAAI,IAAI;AAC5E,SAAQ,iBAAiB,SAAY;AACrC,SAAQ,YAAmC;AAC3C,SAAQ,gBAAiD;AAEvD,SAAK,UAAU,KAAK,QAAQ,KAAK,IAAI;AACrC,SAAK,MAAM,KAAK,IAAI,KAAK,IAAI;AAC7B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AAAA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AM,KAAK,WAAW;AAAA,MACpB,CAAC,SAAS;AACR,iBAAS,IAAI;AAAA,MACf;AAAA,MACA;AAAA,QACE,6BAA6B;AAAA,MAC/B;AAAA,IACF;AAEA,WAAO,MAAM;AACX,iBAAW,OAAO,EAAE;AAAA,IACtB;AAAA,EACF,GAAG,CAAC,UAAU,CAAC;AAEf,SAAO;AACT;","names":["React"]}
@@ -0,0 +1,119 @@
1
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/subscribe/src/index.tsx
2
+ import React from "react";
3
+ function deferred() {
4
+ let resolve = () => void 0;
5
+ let reject = () => void 0;
6
+ let status = "pending";
7
+ const promise = new Promise((res, rej) => {
8
+ resolve = (a) => {
9
+ status = "success";
10
+ const resolveFunc = res;
11
+ resolveFunc(a);
12
+ };
13
+ reject = (error) => {
14
+ status = "failure";
15
+ rej(error);
16
+ };
17
+ });
18
+ return {
19
+ resolve,
20
+ status,
21
+ reject,
22
+ promise
23
+ };
24
+ }
25
+ var NOT_CALLED = Symbol("Subscribe -- Empty Value");
26
+ var Subscribe = class {
27
+ constructor() {
28
+ this.callbacks = /* @__PURE__ */ new Map();
29
+ this.deferredResult = deferred();
30
+ this.lastValue = NOT_CALLED;
31
+ this.resetDeferred = null;
32
+ this.publish = this.publish.bind(this);
33
+ this.add = this.add.bind(this);
34
+ this.remove = this.remove.bind(this);
35
+ }
36
+ /**
37
+ * Trigger the subscriptions using the provided value
38
+ * if there is a reset in progress, wait for it before publishing a new value.
39
+ */
40
+ async publish(val) {
41
+ await this.resetDeferred?.promise;
42
+ this.lastValue = val;
43
+ this.deferredResult.resolve(val);
44
+ this.callbacks.forEach((c) => c(val));
45
+ }
46
+ /**
47
+ * Subscribe to updates
48
+ */
49
+ add(callback, options) {
50
+ const id = this.callbacks.size;
51
+ this.callbacks.set(id, callback);
52
+ if (this.lastValue !== NOT_CALLED && options?.initializeWithPreviousValue === true) {
53
+ callback(this.lastValue);
54
+ }
55
+ return id;
56
+ }
57
+ /**
58
+ * Remove any updates from the given listener
59
+ */
60
+ remove(id) {
61
+ this.callbacks.delete(id);
62
+ }
63
+ /**
64
+ * Reset the state of the listener
65
+ * Passing in a promise will defer resetting the view until the promise is resolved
66
+ */
67
+ async reset(promise) {
68
+ if (promise) {
69
+ this.resetDeferred = deferred();
70
+ await promise;
71
+ }
72
+ if (this.lastValue !== NOT_CALLED) {
73
+ this.deferredResult = deferred();
74
+ }
75
+ this.lastValue = NOT_CALLED;
76
+ this.callbacks.forEach((c) => c(void 0));
77
+ this.resetDeferred?.resolve();
78
+ this.resetDeferred = null;
79
+ }
80
+ /**
81
+ * _Throws_ a promise if the value is still pending
82
+ * Otherwise returns it
83
+ */
84
+ suspend() {
85
+ if (this.lastValue === NOT_CALLED) {
86
+ throw this.deferredResult.promise;
87
+ }
88
+ return this.lastValue;
89
+ }
90
+ /** Get the current value of the subscription */
91
+ get() {
92
+ if (this.lastValue === NOT_CALLED) {
93
+ return void 0;
94
+ }
95
+ return this.lastValue;
96
+ }
97
+ };
98
+ function useSubscribedState(subscriber) {
99
+ const [state, setState] = React.useState(subscriber.get());
100
+ React.useEffect(() => {
101
+ const id = subscriber.add(
102
+ (resp) => {
103
+ setState(resp);
104
+ },
105
+ {
106
+ initializeWithPreviousValue: true
107
+ }
108
+ );
109
+ return () => {
110
+ subscriber.remove(id);
111
+ };
112
+ }, [subscriber]);
113
+ return state;
114
+ }
115
+ export {
116
+ Subscribe,
117
+ useSubscribedState
118
+ };
119
+ //# sourceMappingURL=index.mjs.map
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/subscribe/src/index.tsx
2
+ import React from "react";
3
+ function deferred() {
4
+ let resolve = () => void 0;
5
+ let reject = () => void 0;
6
+ let status = "pending";
7
+ const promise = new Promise((res, rej) => {
8
+ resolve = (a) => {
9
+ status = "success";
10
+ const resolveFunc = res;
11
+ resolveFunc(a);
12
+ };
13
+ reject = (error) => {
14
+ status = "failure";
15
+ rej(error);
16
+ };
17
+ });
18
+ return {
19
+ resolve,
20
+ status,
21
+ reject,
22
+ promise
23
+ };
24
+ }
25
+ var NOT_CALLED = Symbol("Subscribe -- Empty Value");
26
+ var Subscribe = class {
27
+ constructor() {
28
+ this.callbacks = /* @__PURE__ */ new Map();
29
+ this.deferredResult = deferred();
30
+ this.lastValue = NOT_CALLED;
31
+ this.resetDeferred = null;
32
+ this.publish = this.publish.bind(this);
33
+ this.add = this.add.bind(this);
34
+ this.remove = this.remove.bind(this);
35
+ }
36
+ /**
37
+ * Trigger the subscriptions using the provided value
38
+ * if there is a reset in progress, wait for it before publishing a new value.
39
+ */
40
+ async publish(val) {
41
+ await this.resetDeferred?.promise;
42
+ this.lastValue = val;
43
+ this.deferredResult.resolve(val);
44
+ this.callbacks.forEach((c) => c(val));
45
+ }
46
+ /**
47
+ * Subscribe to updates
48
+ */
49
+ add(callback, options) {
50
+ const id = this.callbacks.size;
51
+ this.callbacks.set(id, callback);
52
+ if (this.lastValue !== NOT_CALLED && options?.initializeWithPreviousValue === true) {
53
+ callback(this.lastValue);
54
+ }
55
+ return id;
56
+ }
57
+ /**
58
+ * Remove any updates from the given listener
59
+ */
60
+ remove(id) {
61
+ this.callbacks.delete(id);
62
+ }
63
+ /**
64
+ * Reset the state of the listener
65
+ * Passing in a promise will defer resetting the view until the promise is resolved
66
+ */
67
+ async reset(promise) {
68
+ if (promise) {
69
+ this.resetDeferred = deferred();
70
+ await promise;
71
+ }
72
+ if (this.lastValue !== NOT_CALLED) {
73
+ this.deferredResult = deferred();
74
+ }
75
+ this.lastValue = NOT_CALLED;
76
+ this.callbacks.forEach((c) => c(void 0));
77
+ this.resetDeferred?.resolve();
78
+ this.resetDeferred = null;
79
+ }
80
+ /**
81
+ * _Throws_ a promise if the value is still pending
82
+ * Otherwise returns it
83
+ */
84
+ suspend() {
85
+ if (this.lastValue === NOT_CALLED) {
86
+ throw this.deferredResult.promise;
87
+ }
88
+ return this.lastValue;
89
+ }
90
+ /** Get the current value of the subscription */
91
+ get() {
92
+ if (this.lastValue === NOT_CALLED) {
93
+ return void 0;
94
+ }
95
+ return this.lastValue;
96
+ }
97
+ };
98
+ function useSubscribedState(subscriber) {
99
+ const [state, setState] = React.useState(subscriber.get());
100
+ React.useEffect(() => {
101
+ const id = subscriber.add(
102
+ (resp) => {
103
+ setState(resp);
104
+ },
105
+ {
106
+ initializeWithPreviousValue: true
107
+ }
108
+ );
109
+ return () => {
110
+ subscriber.remove(id);
111
+ };
112
+ }, [subscriber]);
113
+ return state;
114
+ }
115
+ export {
116
+ Subscribe,
117
+ useSubscribedState
118
+ };
119
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/subscribe/src/index.tsx"],"sourcesContent":["import React from \"react\";\n\nexport type SubscribeID = number;\n\ntype ResolveType<T> = (arg?: T) => void;\ntype RejectType = (error?: Error) => void;\ntype StatusType = \"success\" | \"failure\" | \"pending\";\ntype DefferedReturnType<T> = {\n /** a function to resolve the promise */\n resolve: ResolveType<T>;\n\n /** a function to reject the promise */\n reject: RejectType;\n\n /** the status of the promise */\n status: StatusType;\n\n /** a promise to express the above */\n promise: Promise<T>;\n};\n\n/** create a deferred promise */\nfunction deferred<T>(): DefferedReturnType<T> {\n /** the default resolve handler is a noop */\n let resolve: ResolveType<T> = () => undefined;\n\n /** the default reject handler is a noop */\n let reject: RejectType = () => undefined;\n\n let status: StatusType = \"pending\";\n\n const promise = new Promise<T>((res, rej) => {\n resolve = (a?: T) => {\n status = \"success\";\n const resolveFunc = res as ResolveType<T>;\n resolveFunc(a);\n };\n\n reject = (error?: Error) => {\n status = \"failure\";\n rej(error);\n };\n });\n\n return {\n resolve,\n status,\n reject,\n promise,\n };\n}\n\nconst NOT_CALLED = Symbol(\"Subscribe -- Empty Value\");\n/**\n * A pub-sub module that works across the React bridge\n */\nexport class Subscribe<T> {\n private callbacks: Map<SubscribeID, (val: T | undefined) => void> = new Map();\n private deferredResult = deferred<T>();\n private lastValue: T | typeof NOT_CALLED = NOT_CALLED;\n private resetDeferred: DefferedReturnType<void> | null = null;\n constructor() {\n this.publish = this.publish.bind(this);\n this.add = this.add.bind(this);\n this.remove = this.remove.bind(this);\n }\n\n /**\n * Trigger the subscriptions using the provided value\n * if there is a reset in progress, wait for it before publishing a new value.\n */\n async publish(val: T): Promise<void> {\n await this.resetDeferred?.promise;\n this.lastValue = val;\n this.deferredResult.resolve(val);\n this.callbacks.forEach((c) => c(val));\n }\n\n /**\n * Subscribe to updates\n */\n add(\n callback: (arg: T | undefined) => void,\n options?: {\n /** Use the last updated value for this subscription to immediately trigger the onSet callback */\n initializeWithPreviousValue?: boolean;\n },\n ): SubscribeID {\n const id = this.callbacks.size;\n this.callbacks.set(id, callback);\n\n if (\n this.lastValue !== NOT_CALLED &&\n options?.initializeWithPreviousValue === true\n ) {\n callback(this.lastValue);\n }\n\n return id;\n }\n\n /**\n * Remove any updates from the given listener\n */\n remove(id: SubscribeID) {\n this.callbacks.delete(id);\n }\n\n /**\n * Reset the state of the listener\n * Passing in a promise will defer resetting the view until the promise is resolved\n */\n async reset(promise?: Promise<void>) {\n if (promise) {\n this.resetDeferred = deferred<void>();\n await promise;\n }\n\n if (this.lastValue !== NOT_CALLED) {\n this.deferredResult = deferred();\n }\n\n this.lastValue = NOT_CALLED;\n this.callbacks.forEach((c) => c(undefined));\n\n this.resetDeferred?.resolve();\n this.resetDeferred = null;\n }\n\n /**\n * _Throws_ a promise if the value is still pending\n * Otherwise returns it\n */\n suspend(): T {\n if (this.lastValue === NOT_CALLED) {\n throw this.deferredResult.promise;\n }\n\n return this.lastValue;\n }\n\n /** Get the current value of the subscription */\n get(): T | undefined {\n if (this.lastValue === NOT_CALLED) {\n return undefined;\n }\n\n return this.lastValue;\n }\n}\n\nexport interface SubscribedStateHookOptions {\n /** if the state should trigger suspense when waiting to resolve */\n suspend?: boolean;\n}\n\n/** Subscribe to a state change event in a react component */\nexport function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined {\n const [state, setState] = React.useState<T | undefined>(subscriber.get());\n\n React.useEffect(() => {\n const id = subscriber.add(\n (resp) => {\n setState(resp);\n },\n {\n initializeWithPreviousValue: true,\n },\n );\n\n return () => {\n subscriber.remove(id);\n };\n }, [subscriber]);\n\n return state;\n}\n"],"mappings":";AAAA,OAAO,WAAW;AAsBlB,SAAS,WAAqC;AAE5C,MAAI,UAA0B,MAAM;AAGpC,MAAI,SAAqB,MAAM;AAE/B,MAAI,SAAqB;AAEzB,QAAM,UAAU,IAAI,QAAW,CAAC,KAAK,QAAQ;AAC3C,cAAU,CAAC,MAAU;AACnB,eAAS;AACT,YAAM,cAAc;AACpB,kBAAY,CAAC;AAAA,IACf;AAEA,aAAS,CAAC,UAAkB;AAC1B,eAAS;AACT,UAAI,KAAK;AAAA,IACX;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,IAAM,aAAa,OAAO,0BAA0B;AAI7C,IAAM,YAAN,MAAmB;AAAA,EAKxB,cAAc;AAJd,SAAQ,YAA4D,oBAAI,IAAI;AAC5E,SAAQ,iBAAiB,SAAY;AACrC,SAAQ,YAAmC;AAC3C,SAAQ,gBAAiD;AAEvD,SAAK,UAAU,KAAK,QAAQ,KAAK,IAAI;AACrC,SAAK,MAAM,KAAK,IAAI,KAAK,IAAI;AAC7B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,QAAQ,KAAuB;AACnC,UAAM,KAAK,eAAe;AAC1B,SAAK,YAAY;AACjB,SAAK,eAAe,QAAQ,GAAG;AAC/B,SAAK,UAAU,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;AAAA,EACtC;AAAA;AAAA;AAAA;AAAA,EAKA,IACE,UACA,SAIa;AACb,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1
  {
2
2
  "name": "@player-ui/react-subscribe",
3
- "version": "0.8.0--canary.307.9645",
4
- "private": false,
5
- "publishConfig": {
6
- "registry": "https://registry.npmjs.org"
7
- },
8
- "peerDependencies": {
9
- "@types/react": "^17.0.25",
10
- "react": "^17.0.2"
3
+ "version": "0.8.0--canary.410.15865",
4
+ "main": "dist/cjs/index.cjs",
5
+ "module": "dist/index.legacy-esm.js",
6
+ "types": "types/index.d.ts",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ "./dist/index.css": "./dist/index.css",
11
+ ".": {
12
+ "types": "./types/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "default": "./dist/cjs/index.cjs"
15
+ }
11
16
  },
17
+ "files": [
18
+ "dist",
19
+ "src",
20
+ "types"
21
+ ],
12
22
  "dependencies": {
13
23
  "p-defer": "^3.0.0",
14
- "@babel/runtime": "7.15.4"
24
+ "tslib": "^2.6.2"
15
25
  },
16
- "main": "dist/index.cjs.js",
17
- "module": "dist/index.esm.js",
18
- "typings": "dist/index.d.ts",
19
- "sideEffects": false,
20
- "license": "MIT",
21
- "repository": {
22
- "type": "git",
23
- "url": "https://github.com/player-ui/player-ui"
24
- },
25
- "bugs": {
26
- "url": "https://github.com/player-ui/player-ui/issues"
27
- },
28
- "homepage": "https://player-ui.github.io",
29
- "contributors": [
30
- {
31
- "name": "Adam Dierkens",
32
- "url": "https://github.com/adierkens"
33
- },
34
- {
35
- "name": "Spencer Hamm",
36
- "url": "https://github.com/spentacular"
37
- },
38
- {
39
- "name": "Harris Borawski",
40
- "url": "https://github.com/hborawski"
41
- },
42
- {
43
- "name": "Jeremiah Zucker",
44
- "url": "https://github.com/sugarmanz"
45
- },
46
- {
47
- "name": "Ketan Reddy",
48
- "url": "https://github.com/KetanReddy"
49
- },
50
- {
51
- "name": "Brocollie08",
52
- "url": "https://github.com/brocollie08"
53
- },
54
- {
55
- "name": "Kelly Harrop",
56
- "url": "https://github.com/kharrop"
57
- },
58
- {
59
- "name": "Alejandro Fimbres",
60
- "url": "https://github.com/lexfm"
61
- },
62
- {
63
- "name": "Rafael Campos",
64
- "url": "https://github.com/rafbcampos"
65
- }
66
- ]
26
+ "peerDependencies": {
27
+ "@types/react": "^18.2.39",
28
+ "react": "^18.2.0"
29
+ }
67
30
  }
@@ -0,0 +1,43 @@
1
+ import { test, vitest, expect } from "vitest";
2
+ import { Subscribe } from ".";
3
+
4
+ test("Passes events to subscriptions", async () => {
5
+ const stateSub = new Subscribe<{
6
+ value: boolean;
7
+ }>();
8
+ const { add, publish } = stateSub;
9
+ const f = vitest.fn();
10
+ add(f);
11
+ publish({ value: true });
12
+
13
+ await vitest.waitFor(() => expect(f).toBeCalledTimes(1));
14
+ expect(f.mock.calls[0][0].value).toBe(true);
15
+ });
16
+
17
+ test("Removes subscriptions", async () => {
18
+ const stateSub = new Subscribe<{
19
+ value: boolean;
20
+ }>();
21
+ const { add, remove, publish } = stateSub;
22
+ const f = vitest.fn();
23
+ const id = add(f);
24
+ remove(id);
25
+ publish({ value: true });
26
+
27
+ await vitest.waitFor(() => expect(f).not.toHaveBeenCalled());
28
+ });
29
+
30
+ test("Calls multiple", async () => {
31
+ const stateSub = new Subscribe<{
32
+ value: boolean;
33
+ }>();
34
+ const { add, publish } = stateSub;
35
+ const f = vitest.fn();
36
+ const g = vitest.fn();
37
+ add(f);
38
+ add(g);
39
+ publish({ value: true });
40
+
41
+ await vitest.waitFor(() => expect(f).toBeCalledTimes(1));
42
+ expect(g).toBeCalledTimes(1);
43
+ });
package/src/index.tsx CHANGED
@@ -1,10 +1,10 @@
1
- import React from 'react';
1
+ import React from "react";
2
2
 
3
3
  export type SubscribeID = number;
4
4
 
5
5
  type ResolveType<T> = (arg?: T) => void;
6
6
  type RejectType = (error?: Error) => void;
7
- type StatusType = 'success' | 'failure' | 'pending';
7
+ type StatusType = "success" | "failure" | "pending";
8
8
  type DefferedReturnType<T> = {
9
9
  /** a function to resolve the promise */
10
10
  resolve: ResolveType<T>;
@@ -27,17 +27,17 @@ function deferred<T>(): DefferedReturnType<T> {
27
27
  /** the default reject handler is a noop */
28
28
  let reject: RejectType = () => undefined;
29
29
 
30
- let status: StatusType = 'pending';
30
+ let status: StatusType = "pending";
31
31
 
32
32
  const promise = new Promise<T>((res, rej) => {
33
33
  resolve = (a?: T) => {
34
- status = 'success';
34
+ status = "success";
35
35
  const resolveFunc = res as ResolveType<T>;
36
36
  resolveFunc(a);
37
37
  };
38
38
 
39
39
  reject = (error?: Error) => {
40
- status = 'failure';
40
+ status = "failure";
41
41
  rej(error);
42
42
  };
43
43
  });
@@ -50,7 +50,7 @@ function deferred<T>(): DefferedReturnType<T> {
50
50
  };
51
51
  }
52
52
 
53
- const NOT_CALLED = Symbol('Subscribe -- Empty Value');
53
+ const NOT_CALLED = Symbol("Subscribe -- Empty Value");
54
54
  /**
55
55
  * A pub-sub module that works across the React bridge
56
56
  */
@@ -84,7 +84,7 @@ export class Subscribe<T> {
84
84
  options?: {
85
85
  /** Use the last updated value for this subscription to immediately trigger the onSet callback */
86
86
  initializeWithPreviousValue?: boolean;
87
- }
87
+ },
88
88
  ): SubscribeID {
89
89
  const id = this.callbacks.size;
90
90
  this.callbacks.set(id, callback);
@@ -165,7 +165,7 @@ export function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined {
165
165
  },
166
166
  {
167
167
  initializeWithPreviousValue: true,
168
- }
168
+ },
169
169
  );
170
170
 
171
171
  return () => {
@@ -1,8 +1,8 @@
1
- declare type SubscribeID = number;
1
+ export type SubscribeID = number;
2
2
  /**
3
3
  * A pub-sub module that works across the React bridge
4
4
  */
5
- declare class Subscribe<T> {
5
+ export declare class Subscribe<T> {
6
6
  private callbacks;
7
7
  private deferredResult;
8
8
  private lastValue;
@@ -37,11 +37,10 @@ declare class Subscribe<T> {
37
37
  /** Get the current value of the subscription */
38
38
  get(): T | undefined;
39
39
  }
40
- interface SubscribedStateHookOptions {
40
+ export interface SubscribedStateHookOptions {
41
41
  /** if the state should trigger suspense when waiting to resolve */
42
42
  suspend?: boolean;
43
43
  }
44
44
  /** Subscribe to a state change event in a react component */
45
- declare function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined;
46
-
47
- export { Subscribe, SubscribeID, SubscribedStateHookOptions, useSubscribedState };
45
+ export declare function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined;
46
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.cjs.js DELETED
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
-
11
- var __async = (__this, __arguments, generator) => {
12
- return new Promise((resolve, reject) => {
13
- var fulfilled = (value) => {
14
- try {
15
- step(generator.next(value));
16
- } catch (e) {
17
- reject(e);
18
- }
19
- };
20
- var rejected = (value) => {
21
- try {
22
- step(generator.throw(value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- };
27
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
28
- step((generator = generator.apply(__this, __arguments)).next());
29
- });
30
- };
31
- function deferred() {
32
- let resolve = () => void 0;
33
- let reject = () => void 0;
34
- let status = "pending";
35
- const promise = new Promise((res, rej) => {
36
- resolve = (a) => {
37
- status = "success";
38
- const resolveFunc = res;
39
- resolveFunc(a);
40
- };
41
- reject = (error) => {
42
- status = "failure";
43
- rej(error);
44
- };
45
- });
46
- return {
47
- resolve,
48
- status,
49
- reject,
50
- promise
51
- };
52
- }
53
- const NOT_CALLED = Symbol("Subscribe -- Empty Value");
54
- class Subscribe {
55
- constructor() {
56
- this.callbacks = new Map();
57
- this.deferredResult = deferred();
58
- this.lastValue = NOT_CALLED;
59
- this.resetDeferred = null;
60
- this.publish = this.publish.bind(this);
61
- this.add = this.add.bind(this);
62
- this.remove = this.remove.bind(this);
63
- }
64
- publish(val) {
65
- return __async(this, null, function* () {
66
- var _a;
67
- yield (_a = this.resetDeferred) == null ? void 0 : _a.promise;
68
- this.lastValue = val;
69
- this.deferredResult.resolve(val);
70
- this.callbacks.forEach((c) => c(val));
71
- });
72
- }
73
- add(callback, options) {
74
- const id = this.callbacks.size;
75
- this.callbacks.set(id, callback);
76
- if (this.lastValue !== NOT_CALLED && (options == null ? void 0 : options.initializeWithPreviousValue) === true) {
77
- callback(this.lastValue);
78
- }
79
- return id;
80
- }
81
- remove(id) {
82
- this.callbacks.delete(id);
83
- }
84
- reset(promise) {
85
- return __async(this, null, function* () {
86
- var _a;
87
- if (promise) {
88
- this.resetDeferred = deferred();
89
- yield promise;
90
- }
91
- if (this.lastValue !== NOT_CALLED) {
92
- this.deferredResult = deferred();
93
- }
94
- this.lastValue = NOT_CALLED;
95
- this.callbacks.forEach((c) => c(void 0));
96
- (_a = this.resetDeferred) == null ? void 0 : _a.resolve();
97
- this.resetDeferred = null;
98
- });
99
- }
100
- suspend() {
101
- if (this.lastValue === NOT_CALLED) {
102
- throw this.deferredResult.promise;
103
- }
104
- return this.lastValue;
105
- }
106
- get() {
107
- if (this.lastValue === NOT_CALLED) {
108
- return void 0;
109
- }
110
- return this.lastValue;
111
- }
112
- }
113
- function useSubscribedState(subscriber) {
114
- const [state, setState] = React__default["default"].useState(subscriber.get());
115
- React__default["default"].useEffect(() => {
116
- const id = subscriber.add((resp) => {
117
- setState(resp);
118
- }, {
119
- initializeWithPreviousValue: true
120
- });
121
- return () => {
122
- subscriber.remove(id);
123
- };
124
- }, [subscriber]);
125
- return state;
126
- }
127
-
128
- exports.Subscribe = Subscribe;
129
- exports.useSubscribedState = useSubscribedState;
130
- //# sourceMappingURL=index.cjs.js.map
package/dist/index.esm.js DELETED
@@ -1,121 +0,0 @@
1
- import React from 'react';
2
-
3
- var __async = (__this, __arguments, generator) => {
4
- return new Promise((resolve, reject) => {
5
- var fulfilled = (value) => {
6
- try {
7
- step(generator.next(value));
8
- } catch (e) {
9
- reject(e);
10
- }
11
- };
12
- var rejected = (value) => {
13
- try {
14
- step(generator.throw(value));
15
- } catch (e) {
16
- reject(e);
17
- }
18
- };
19
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
20
- step((generator = generator.apply(__this, __arguments)).next());
21
- });
22
- };
23
- function deferred() {
24
- let resolve = () => void 0;
25
- let reject = () => void 0;
26
- let status = "pending";
27
- const promise = new Promise((res, rej) => {
28
- resolve = (a) => {
29
- status = "success";
30
- const resolveFunc = res;
31
- resolveFunc(a);
32
- };
33
- reject = (error) => {
34
- status = "failure";
35
- rej(error);
36
- };
37
- });
38
- return {
39
- resolve,
40
- status,
41
- reject,
42
- promise
43
- };
44
- }
45
- const NOT_CALLED = Symbol("Subscribe -- Empty Value");
46
- class Subscribe {
47
- constructor() {
48
- this.callbacks = new Map();
49
- this.deferredResult = deferred();
50
- this.lastValue = NOT_CALLED;
51
- this.resetDeferred = null;
52
- this.publish = this.publish.bind(this);
53
- this.add = this.add.bind(this);
54
- this.remove = this.remove.bind(this);
55
- }
56
- publish(val) {
57
- return __async(this, null, function* () {
58
- var _a;
59
- yield (_a = this.resetDeferred) == null ? void 0 : _a.promise;
60
- this.lastValue = val;
61
- this.deferredResult.resolve(val);
62
- this.callbacks.forEach((c) => c(val));
63
- });
64
- }
65
- add(callback, options) {
66
- const id = this.callbacks.size;
67
- this.callbacks.set(id, callback);
68
- if (this.lastValue !== NOT_CALLED && (options == null ? void 0 : options.initializeWithPreviousValue) === true) {
69
- callback(this.lastValue);
70
- }
71
- return id;
72
- }
73
- remove(id) {
74
- this.callbacks.delete(id);
75
- }
76
- reset(promise) {
77
- return __async(this, null, function* () {
78
- var _a;
79
- if (promise) {
80
- this.resetDeferred = deferred();
81
- yield promise;
82
- }
83
- if (this.lastValue !== NOT_CALLED) {
84
- this.deferredResult = deferred();
85
- }
86
- this.lastValue = NOT_CALLED;
87
- this.callbacks.forEach((c) => c(void 0));
88
- (_a = this.resetDeferred) == null ? void 0 : _a.resolve();
89
- this.resetDeferred = null;
90
- });
91
- }
92
- suspend() {
93
- if (this.lastValue === NOT_CALLED) {
94
- throw this.deferredResult.promise;
95
- }
96
- return this.lastValue;
97
- }
98
- get() {
99
- if (this.lastValue === NOT_CALLED) {
100
- return void 0;
101
- }
102
- return this.lastValue;
103
- }
104
- }
105
- function useSubscribedState(subscriber) {
106
- const [state, setState] = React.useState(subscriber.get());
107
- React.useEffect(() => {
108
- const id = subscriber.add((resp) => {
109
- setState(resp);
110
- }, {
111
- initializeWithPreviousValue: true
112
- });
113
- return () => {
114
- subscriber.remove(id);
115
- };
116
- }, [subscriber]);
117
- return state;
118
- }
119
-
120
- export { Subscribe, useSubscribedState };
121
- //# sourceMappingURL=index.esm.js.map