zustand-yjs
Advanced tools
Comparing version 0.0.8 to 0.0.9
114
index.cjs.js
@@ -9,9 +9,32 @@ 'use strict'; | ||
var Y = require('yjs'); | ||
var _extends = require('@babel/runtime/helpers/extends'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
n['default'] = e; | ||
return Object.freeze(n); | ||
} | ||
var create__default = /*#__PURE__*/_interopDefaultLegacy(create); | ||
var shallow__default = /*#__PURE__*/_interopDefaultLegacy(shallow); | ||
var Y__namespace = /*#__PURE__*/_interopNamespace(Y); | ||
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends); | ||
var YDocEnum; | ||
exports.YDocEnum = void 0; | ||
@@ -24,5 +47,5 @@ (function (YDocEnum) { | ||
YDocEnum[YDocEnum["USE_CNT"] = 4] = "USE_CNT"; | ||
})(YDocEnum || (YDocEnum = {})); | ||
})(exports.YDocEnum || (exports.YDocEnum = {})); | ||
var ObserveEnum; | ||
exports.ObserveEnum = void 0; | ||
@@ -33,7 +56,25 @@ (function (ObserveEnum) { | ||
ObserveEnum[ObserveEnum["USE_CNT"] = 2] = "USE_CNT"; | ||
})(ObserveEnum || (ObserveEnum = {})); | ||
})(exports.ObserveEnum || (exports.ObserveEnum = {})); | ||
var newStartAwareness = function newStartAwareness(setAwareness) { | ||
var startAwareness = function startAwareness(provider) { | ||
var awareness = provider.awareness; | ||
var updateAwareness = function updateAwareness() { | ||
setAwareness(provider, Array.from(awareness.getStates().values())); | ||
}; | ||
awareness.on('update', updateAwareness); | ||
return function () { | ||
awareness.off('update', updateAwareness); | ||
}; | ||
}; | ||
return startAwareness; | ||
}; | ||
var useYStore = create__default['default'](function (set, get) { | ||
return { | ||
yDocs: [], | ||
yAwareness: [], | ||
observers: [], | ||
@@ -61,3 +102,3 @@ data: [], | ||
return obs !== match; | ||
}), [[match[ObserveEnum.TYPE], match[ObserveEnum.OBSERVE], match[ObserveEnum.USE_CNT] + 1]]) | ||
}), [[match[exports.ObserveEnum.TYPE], match[exports.ObserveEnum.OBSERVE], match[exports.ObserveEnum.USE_CNT] + 1]]) | ||
}); | ||
@@ -83,3 +124,3 @@ } else { | ||
var observerCount = match[ObserveEnum.USE_CNT] - 1; | ||
var observerCount = match[exports.ObserveEnum.USE_CNT] - 1; | ||
@@ -90,6 +131,6 @@ if (observerCount > 0) { | ||
return obs !== match; | ||
}), [[match[ObserveEnum.TYPE], match[ObserveEnum.OBSERVE], observerCount]]) | ||
}), [[match[exports.ObserveEnum.TYPE], match[exports.ObserveEnum.OBSERVE], observerCount]]) | ||
}); | ||
} else { | ||
type.unobserve(match[ObserveEnum.OBSERVE]); | ||
type.unobserve(match[exports.ObserveEnum.OBSERVE]); | ||
set({ | ||
@@ -108,2 +149,3 @@ observers: [].concat(observers.filter(function (obs) { | ||
var yDocs = get().yDocs; | ||
var yAwareness = get().yAwareness; | ||
var match = yDocs.find(function (_ref5) { | ||
@@ -119,3 +161,3 @@ var doc = _ref5[1]; | ||
var observerCount = match[YDocEnum.USE_CNT] - 1; | ||
var observerCount = match[exports.YDocEnum.USE_CNT] - 1; | ||
@@ -126,6 +168,6 @@ if (observerCount > 0) { | ||
return yDoc !== match; | ||
}), [[match[YDocEnum.GUID], match[YDocEnum.DOC], match[YDocEnum.MOUNT], match[YDocEnum.UN_MOUNT], observerCount]]) | ||
}), [[match[exports.YDocEnum.GUID], match[exports.YDocEnum.DOC], match[exports.YDocEnum.MOUNT], match[exports.YDocEnum.UN_MOUNT], observerCount]]) | ||
}); | ||
} else { | ||
var unMount = match[YDocEnum.UN_MOUNT]; | ||
var unMount = match[exports.YDocEnum.UN_MOUNT]; | ||
if (unMount) unMount(); | ||
@@ -135,2 +177,6 @@ set({ | ||
return yDoc !== match; | ||
}), | ||
yAwareness: yAwareness.filter(function (_ref6) { | ||
var yDocName = _ref6[0]; | ||
return yDocName !== match[0]; | ||
}) | ||
@@ -142,4 +188,5 @@ }); | ||
var yDocs = get().yDocs; | ||
var match = yDocs.find(function (_ref6) { | ||
var name = _ref6[0]; | ||
var yAwareness = get().yAwareness; | ||
var match = yDocs.find(function (_ref7) { | ||
var name = _ref7[0]; | ||
return name === yDoc.guid; | ||
@@ -149,10 +196,10 @@ }); | ||
if (match) { | ||
if (match[YDocEnum.MOUNT] !== mount) { | ||
if (match[exports.YDocEnum.MOUNT] !== mount) { | ||
console.warn('A different connection function was already provided for ', name, '. This unMount function will be ignored'); | ||
} | ||
match[YDocEnum.USE_CNT]++; | ||
match[exports.YDocEnum.USE_CNT]++; | ||
set({ | ||
yDocs: [].concat(yDocs.filter(function (_ref7) { | ||
var name = _ref7[0]; | ||
yDocs: [].concat(yDocs.filter(function (_ref8) { | ||
var name = _ref8[0]; | ||
return name !== yDoc.guid; | ||
@@ -162,3 +209,7 @@ }), [match]) | ||
} else { | ||
var unMount = mount(yDoc); | ||
var unMount = mount(yDoc, newStartAwareness(function (protocol, changes) { | ||
set({ | ||
yAwareness: [].concat(yAwareness, [[yDoc.guid, protocol.awareness, changes]]) | ||
}); | ||
})); | ||
set({ | ||
@@ -274,4 +325,4 @@ yDocs: [].concat(yDocs, [[yDoc.guid, yDoc, mount, unMount, 1]]) | ||
}); | ||
if (match) return match[YDocEnum.DOC]; | ||
var yDoc = new Y.Doc(); | ||
if (match) return match[exports.YDocEnum.DOC]; | ||
var yDoc = new Y__namespace.Doc(); | ||
yDoc.guid = name; | ||
@@ -289,4 +340,27 @@ mountYDoc(yDoc, mount); | ||
var useYAwareness = function useYAwareness(yDoc) { | ||
var matches = useYStore(react.useCallback(function (state) { | ||
return state.yAwareness.filter(function (_ref) { | ||
var guid = _ref[0]; | ||
return guid === yDoc.guid; | ||
}); | ||
}, [yDoc])); | ||
if (matches.length === 0) return [[], function () { | ||
console.error('Awareness not registered. Use startAwareness method from your YDoc connect function.'); | ||
}]; | ||
var _matches$ = matches[0]; | ||
_matches$[0]; | ||
var awareness = _matches$[1], | ||
changes = _matches$[2]; | ||
return [changes, function (newState) { | ||
var previousState = awareness.getLocalState(); | ||
awareness.setLocalState(_extends__default['default']({}, previousState, { | ||
newState: newState | ||
})); | ||
}]; | ||
}; | ||
exports.useYArray = useYArray; | ||
exports.useYAwareness = useYAwareness; | ||
exports.useYDoc = useYDoc; | ||
exports.useYMap = useYMap; |
export { default as useYArray } from './useYArray'; | ||
export { default as useYMap } from './useYMap'; | ||
export { default as useYDoc } from './useYDoc'; | ||
export { default as useYAwareness } from './useYAwareness'; | ||
export * from './types'; |
@@ -1,2 +0,2 @@ | ||
var zustandYjs = (function (exports, create, react, shallow, Y) { | ||
var zustandYjs = (function (exports, create, react, shallow, Y, _extends) { | ||
'use strict'; | ||
@@ -6,6 +6,28 @@ | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
n['default'] = e; | ||
return Object.freeze(n); | ||
} | ||
var create__default = /*#__PURE__*/_interopDefaultLegacy(create); | ||
var shallow__default = /*#__PURE__*/_interopDefaultLegacy(shallow); | ||
var Y__namespace = /*#__PURE__*/_interopNamespace(Y); | ||
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends); | ||
var YDocEnum; | ||
exports.YDocEnum = void 0; | ||
@@ -18,5 +40,5 @@ (function (YDocEnum) { | ||
YDocEnum[YDocEnum["USE_CNT"] = 4] = "USE_CNT"; | ||
})(YDocEnum || (YDocEnum = {})); | ||
})(exports.YDocEnum || (exports.YDocEnum = {})); | ||
var ObserveEnum; | ||
exports.ObserveEnum = void 0; | ||
@@ -27,7 +49,25 @@ (function (ObserveEnum) { | ||
ObserveEnum[ObserveEnum["USE_CNT"] = 2] = "USE_CNT"; | ||
})(ObserveEnum || (ObserveEnum = {})); | ||
})(exports.ObserveEnum || (exports.ObserveEnum = {})); | ||
var newStartAwareness = function newStartAwareness(setAwareness) { | ||
var startAwareness = function startAwareness(provider) { | ||
var awareness = provider.awareness; | ||
var updateAwareness = function updateAwareness() { | ||
setAwareness(provider, Array.from(awareness.getStates().values())); | ||
}; | ||
awareness.on('update', updateAwareness); | ||
return function () { | ||
awareness.off('update', updateAwareness); | ||
}; | ||
}; | ||
return startAwareness; | ||
}; | ||
var useYStore = create__default['default'](function (set, get) { | ||
return { | ||
yDocs: [], | ||
yAwareness: [], | ||
observers: [], | ||
@@ -55,3 +95,3 @@ data: [], | ||
return obs !== match; | ||
}), [[match[ObserveEnum.TYPE], match[ObserveEnum.OBSERVE], match[ObserveEnum.USE_CNT] + 1]]) | ||
}), [[match[exports.ObserveEnum.TYPE], match[exports.ObserveEnum.OBSERVE], match[exports.ObserveEnum.USE_CNT] + 1]]) | ||
}); | ||
@@ -77,3 +117,3 @@ } else { | ||
var observerCount = match[ObserveEnum.USE_CNT] - 1; | ||
var observerCount = match[exports.ObserveEnum.USE_CNT] - 1; | ||
@@ -84,6 +124,6 @@ if (observerCount > 0) { | ||
return obs !== match; | ||
}), [[match[ObserveEnum.TYPE], match[ObserveEnum.OBSERVE], observerCount]]) | ||
}), [[match[exports.ObserveEnum.TYPE], match[exports.ObserveEnum.OBSERVE], observerCount]]) | ||
}); | ||
} else { | ||
type.unobserve(match[ObserveEnum.OBSERVE]); | ||
type.unobserve(match[exports.ObserveEnum.OBSERVE]); | ||
set({ | ||
@@ -102,2 +142,3 @@ observers: [].concat(observers.filter(function (obs) { | ||
var yDocs = get().yDocs; | ||
var yAwareness = get().yAwareness; | ||
var match = yDocs.find(function (_ref5) { | ||
@@ -113,3 +154,3 @@ var doc = _ref5[1]; | ||
var observerCount = match[YDocEnum.USE_CNT] - 1; | ||
var observerCount = match[exports.YDocEnum.USE_CNT] - 1; | ||
@@ -120,6 +161,6 @@ if (observerCount > 0) { | ||
return yDoc !== match; | ||
}), [[match[YDocEnum.GUID], match[YDocEnum.DOC], match[YDocEnum.MOUNT], match[YDocEnum.UN_MOUNT], observerCount]]) | ||
}), [[match[exports.YDocEnum.GUID], match[exports.YDocEnum.DOC], match[exports.YDocEnum.MOUNT], match[exports.YDocEnum.UN_MOUNT], observerCount]]) | ||
}); | ||
} else { | ||
var unMount = match[YDocEnum.UN_MOUNT]; | ||
var unMount = match[exports.YDocEnum.UN_MOUNT]; | ||
if (unMount) unMount(); | ||
@@ -129,2 +170,6 @@ set({ | ||
return yDoc !== match; | ||
}), | ||
yAwareness: yAwareness.filter(function (_ref6) { | ||
var yDocName = _ref6[0]; | ||
return yDocName !== match[0]; | ||
}) | ||
@@ -136,4 +181,5 @@ }); | ||
var yDocs = get().yDocs; | ||
var match = yDocs.find(function (_ref6) { | ||
var name = _ref6[0]; | ||
var yAwareness = get().yAwareness; | ||
var match = yDocs.find(function (_ref7) { | ||
var name = _ref7[0]; | ||
return name === yDoc.guid; | ||
@@ -143,10 +189,10 @@ }); | ||
if (match) { | ||
if (match[YDocEnum.MOUNT] !== mount) { | ||
if (match[exports.YDocEnum.MOUNT] !== mount) { | ||
console.warn('A different connection function was already provided for ', name, '. This unMount function will be ignored'); | ||
} | ||
match[YDocEnum.USE_CNT]++; | ||
match[exports.YDocEnum.USE_CNT]++; | ||
set({ | ||
yDocs: [].concat(yDocs.filter(function (_ref7) { | ||
var name = _ref7[0]; | ||
yDocs: [].concat(yDocs.filter(function (_ref8) { | ||
var name = _ref8[0]; | ||
return name !== yDoc.guid; | ||
@@ -156,3 +202,7 @@ }), [match]) | ||
} else { | ||
var unMount = mount(yDoc); | ||
var unMount = mount(yDoc, newStartAwareness(function (protocol, changes) { | ||
set({ | ||
yAwareness: [].concat(yAwareness, [[yDoc.guid, protocol.awareness, changes]]) | ||
}); | ||
})); | ||
set({ | ||
@@ -268,4 +318,4 @@ yDocs: [].concat(yDocs, [[yDoc.guid, yDoc, mount, unMount, 1]]) | ||
}); | ||
if (match) return match[YDocEnum.DOC]; | ||
var yDoc = new Y.Doc(); | ||
if (match) return match[exports.YDocEnum.DOC]; | ||
var yDoc = new Y__namespace.Doc(); | ||
yDoc.guid = name; | ||
@@ -283,3 +333,26 @@ mountYDoc(yDoc, mount); | ||
var useYAwareness = function useYAwareness(yDoc) { | ||
var matches = useYStore(react.useCallback(function (state) { | ||
return state.yAwareness.filter(function (_ref) { | ||
var guid = _ref[0]; | ||
return guid === yDoc.guid; | ||
}); | ||
}, [yDoc])); | ||
if (matches.length === 0) return [[], function () { | ||
console.error('Awareness not registered. Use startAwareness method from your YDoc connect function.'); | ||
}]; | ||
var _matches$ = matches[0]; | ||
_matches$[0]; | ||
var awareness = _matches$[1], | ||
changes = _matches$[2]; | ||
return [changes, function (newState) { | ||
var previousState = awareness.getLocalState(); | ||
awareness.setLocalState(_extends__default['default']({}, previousState, { | ||
newState: newState | ||
})); | ||
}]; | ||
}; | ||
exports.useYArray = useYArray; | ||
exports.useYAwareness = useYAwareness; | ||
exports.useYDoc = useYDoc; | ||
@@ -292,2 +365,2 @@ exports.useYMap = useYMap; | ||
}({}, zustand, react, shallow, Y)); | ||
}({}, zustand, react, shallow, Y, _extends)); |
69
index.js
import create from 'zustand'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { useEffect, useMemo, useCallback } from 'react'; | ||
import shallow from 'zustand/shallow'; | ||
import { Doc } from 'yjs'; | ||
import * as Y from 'yjs'; | ||
@@ -24,5 +24,23 @@ var YDocEnum; | ||
const newStartAwareness = setAwareness => { | ||
const startAwareness = provider => { | ||
const awareness = provider.awareness; | ||
const updateAwareness = () => { | ||
setAwareness(provider, Array.from(awareness.getStates().values())); | ||
}; | ||
awareness.on('update', updateAwareness); | ||
return () => { | ||
awareness.off('update', updateAwareness); | ||
}; | ||
}; | ||
return startAwareness; | ||
}; | ||
const useYStore = create((set, get) => { | ||
return { | ||
yDocs: [], | ||
yAwareness: [], | ||
observers: [], | ||
@@ -80,2 +98,3 @@ data: [], | ||
const yDocs = get().yDocs; | ||
const yAwareness = get().yAwareness; | ||
const match = yDocs.find(([, doc]) => doc === yDoc); | ||
@@ -98,3 +117,4 @@ | ||
set({ | ||
yDocs: yDocs.filter(yDoc => yDoc !== match) | ||
yDocs: yDocs.filter(yDoc => yDoc !== match), | ||
yAwareness: yAwareness.filter(([yDocName]) => yDocName !== match[0]) | ||
}); | ||
@@ -106,2 +126,3 @@ } | ||
const yDocs = get().yDocs; | ||
const yAwareness = get().yAwareness; | ||
const match = yDocs.find(([name]) => name === yDoc.guid); | ||
@@ -119,3 +140,7 @@ | ||
} else { | ||
const unMount = mount(yDoc); | ||
const unMount = mount(yDoc, newStartAwareness((protocol, changes) => { | ||
set({ | ||
yAwareness: [...yAwareness, [yDoc.guid, protocol.awareness, changes]] | ||
}); | ||
})); | ||
set({ | ||
@@ -196,3 +221,3 @@ yDocs: [...yDocs, [yDoc.guid, yDoc, mount, unMount, 1]] | ||
if (match) return match[YDocEnum.DOC]; | ||
const yDoc = new Doc(); | ||
const yDoc = new Y.Doc(); | ||
yDoc.guid = name; | ||
@@ -208,2 +233,34 @@ mountYDoc(yDoc, mount); | ||
export { useYArray, useYDoc, useYMap }; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
const useYAwareness = yDoc => { | ||
const matches = useYStore(useCallback(state => state.yAwareness.filter(([guid]) => guid === yDoc.guid), [yDoc])); | ||
if (matches.length === 0) return [[], () => { | ||
console.error('Awareness not registered. Use startAwareness method from your YDoc connect function.'); | ||
}]; | ||
const [_docGuid, awareness, changes] = matches[0]; | ||
return [changes, newState => { | ||
const previousState = awareness.getLocalState(); | ||
awareness.setLocalState(_extends({}, previousState, { | ||
newState | ||
})); | ||
}]; | ||
}; | ||
export { ObserveEnum, YDocEnum, useYArray, useYAwareness, useYDoc, useYMap }; |
{ | ||
"name": "zustand-yjs", | ||
"private": false, | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Zustand Stores with Yjs", | ||
@@ -29,2 +29,3 @@ "main": "./index.cjs.js", | ||
"peerDependencies": { | ||
"y-protocols": ">=1.0.4", | ||
"yjs": ">=13.0.0", | ||
@@ -31,0 +32,0 @@ "zustand": ">=3.3.1" |
import * as Y from 'yjs'; | ||
import { MountFunction } from './useYStore'; | ||
import { MountFunction } from './types'; | ||
declare const useYDoc: (name: string, mount: MountFunction) => Y.Doc; | ||
export default useYDoc; |
@@ -1,28 +0,3 @@ | ||
import * as Y from 'yjs'; | ||
declare type ObserveFunc = (arg0: any, arg1: Y.Transaction) => void; | ||
declare type UnMountFunction = () => void | void; | ||
export declare type MountFunction = (doc: Y.Doc) => UnMountFunction; | ||
export declare enum YDocEnum { | ||
GUID = 0, | ||
DOC = 1, | ||
MOUNT = 2, | ||
UN_MOUNT = 3, | ||
USE_CNT = 4 | ||
} | ||
export declare enum ObserveEnum { | ||
TYPE = 0, | ||
OBSERVE = 1, | ||
USE_CNT = 2 | ||
} | ||
declare type YStore = { | ||
yDocs: [string, Y.Doc, MountFunction, UnMountFunction, number][]; | ||
observers: [Y.AbstractType<any>, ObserveFunc, number][]; | ||
data: [Y.AbstractType<any>, any][]; | ||
listenType(type: Y.AbstractType<any>, listen: ObserveFunc): void; | ||
unListenType(type: Y.AbstractType<any>): void; | ||
unMountYDoc(yDoc: Y.Doc): void; | ||
mountYDoc(yDoc: Y.Doc, mount: MountFunction): void; | ||
update(type: Y.AbstractType<any>, data: any): void; | ||
}; | ||
import { YStore } from './types'; | ||
declare const useYStore: import("zustand").UseStore<YStore>; | ||
export default useYStore; |
38665
13
916
3