@diplodoc/utils
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,4 @@ | ||
import { AttrsParser } from './lib/attrs'; | ||
export { AttrsParser }; | ||
import { AttrsParser } from './attrs'; | ||
import { ControllerLoadedCallback, CreateLoadQueueArgs, ScriptStore, createLoadQueue, getQueueStore, getScriptStore, handleQueueCreated, isBrowser, useController } from './extension-load-queue'; | ||
export { AttrsParser, createLoadQueue, getQueueStore, getScriptStore, handleQueueCreated, isBrowser, useController, }; | ||
export type { ControllerLoadedCallback, CreateLoadQueueArgs, ScriptStore }; |
149
lib/index.js
@@ -1,18 +0,16 @@ | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __objRest = (source, exclude) => { | ||
var target = {}; | ||
for (var prop in source) | ||
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) | ||
target[prop] = source[prop]; | ||
if (source != null && __getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(source)) { | ||
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) | ||
target[prop] = source[prop]; | ||
} | ||
return target; | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __accessCheck = (obj, member, msg) => { | ||
@@ -36,10 +34,23 @@ if (!member.has(obj)) | ||
}; | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
AttrsParser: () => AttrsParser | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/lib/attrs.ts | ||
@@ -87,3 +98,3 @@ var _key, _pending, _isInsideQuotation, _didQuotationClosed, _currentKeyType, _selectors, _handlers; | ||
apply(target) { | ||
const { attr: singleKeyAttrs = [], ...fullAttrs } = this.state; | ||
const _a = this.state, { attr: singleKeyAttrs = [] } = _a, fullAttrs = __objRest(_a, ["attr"]); | ||
for (const [property, values] of Object.entries(fullAttrs)) { | ||
@@ -216,5 +227,89 @@ target.attrJoin(property, values.join(" ")); | ||
_handlers = new WeakMap(); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
AttrsParser | ||
}); | ||
// src/lib/extension-load-queue.ts | ||
import { useEffect, useState } from "react"; | ||
var QUEUE_SYMBOL = Symbol.for("queue"); | ||
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined"; | ||
var getScriptStore = (key) => { | ||
if (isBrowser()) { | ||
window[key] = window[key] || []; | ||
return window[key]; | ||
} else { | ||
throw new Error("This functionality should be employed on the client-side."); | ||
} | ||
}; | ||
var getQueueStore = () => { | ||
if (isBrowser()) { | ||
window[QUEUE_SYMBOL] = window[QUEUE_SYMBOL] || false; | ||
return window[QUEUE_SYMBOL]; | ||
} | ||
return null; | ||
}; | ||
var handleQueueCreated = (created) => { | ||
window[QUEUE_SYMBOL] = created; | ||
}; | ||
var createLoadQueue = ({ | ||
store, | ||
createController, | ||
isQueueCreated = getQueueStore(), | ||
onQueueCreated = handleQueueCreated | ||
}) => { | ||
if (!store || isQueueCreated) { | ||
return; | ||
} | ||
onQueueCreated(true); | ||
const controller = createController(); | ||
const queue = store.splice(0, store.length); | ||
store.push = function(...args) { | ||
args.forEach((callback) => { | ||
queue.push(callback); | ||
unqueue(); | ||
}); | ||
return queue.length; | ||
}; | ||
let processing = false; | ||
function unqueue() { | ||
if (!processing) { | ||
next(); | ||
} | ||
} | ||
function next() { | ||
return __async(this, null, function* () { | ||
processing = true; | ||
const callback = queue.shift(); | ||
if (callback) { | ||
yield callback(controller); | ||
return next(); | ||
} | ||
processing = false; | ||
}); | ||
} | ||
unqueue(); | ||
}; | ||
var noop = () => { | ||
}; | ||
function useController(store) { | ||
const [controller, setController] = useState(null); | ||
useEffect(() => { | ||
if (store) { | ||
store.push(setController); | ||
return () => { | ||
const index = store.indexOf(setController); | ||
if (index > -1) { | ||
store.splice(index, 1); | ||
} | ||
}; | ||
} | ||
return noop; | ||
}, []); | ||
return controller; | ||
} | ||
export { | ||
AttrsParser, | ||
createLoadQueue, | ||
getQueueStore, | ||
getScriptStore, | ||
handleQueueCreated, | ||
isBrowser, | ||
useController | ||
}; |
{ | ||
"name": "@diplodoc/utils", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Diplodoc cross-packages utils", | ||
@@ -16,2 +16,8 @@ "homepage": "https://github.com/diplodoc-platform/utils", | ||
"types": "./lib/index.d.js", | ||
"exports": { | ||
".": { | ||
"types": "./lib/index.d.ts", | ||
"default": "./lib/index.js" | ||
} | ||
}, | ||
"files": [ | ||
@@ -24,3 +30,3 @@ "lib/" | ||
"build": "npm run build:clean && npm run build:code", | ||
"build:code": "tsc -p tsconfig.types.json && node scripts/bundle.js", | ||
"build:code": "tsc -p tsconfig.build.json && node scripts/bundle.js", | ||
"build:clean": "rm -rf lib", | ||
@@ -43,2 +49,3 @@ "prepublishOnly": "npm run build", | ||
"@types/node": "^17.0.35", | ||
"@types/react": "^18.3.5", | ||
"esbuild": "^0.17.18", | ||
@@ -48,4 +55,13 @@ "esbuild-jest": "^0.5.0", | ||
"jest": "^28.1.0", | ||
"react": "^18.3.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"react": { | ||
"optional": true | ||
} | ||
} | ||
} |
@@ -14,2 +14,3 @@ # Diplodoc utils | ||
- [AttrsParser](#AttrsParser) | ||
- [Extension Load Queue](#Extension Load Queue) | ||
@@ -25,15 +26,35 @@ ### AttrsParser | ||
```typescript | ||
/* | ||
optional first query | ||
if provided parser will parse it immediately | ||
each 'parse' call is pure | ||
*/ | ||
const attrs = new AttrsParser('{.class #id data-name=diplodoc}'); | ||
attrs.state /* { class: ['class'], id: ['id'], 'data-name': ['diplodoc'] } */ | ||
/* | ||
optional first query | ||
if provided parser will parse it immediately | ||
each 'parse' call is pure | ||
*/ | ||
const attrs = new AttrsParser('{.class #id data-name=diplodoc}'); | ||
const other = attrs.parse('{data-wide title="Support quotes too"}') | ||
attrs.state /* { class: ['class'], id: ['id'], 'data-name': ['diplodoc'] } */ | ||
other /* { attr: ['data-wide'], title: ['Support quotes too'] } */ | ||
const other = attrs.parse('{data-wide title="Support quotes too"}') | ||
other /* { attr: ['data-wide'], title: ['Support quotes too'] } */ | ||
``` | ||
### Extension Load Queue | ||
### Purpose | ||
This queue mechanism allows asynchronous loading of extensions by setting a property on the "window" object in the browser. This property has a Symbol data type, which makes it difficult for external manipulations to access it. | ||
### Interface | ||
```typescript | ||
// create a unique symbol | ||
export const GLOBAL_SYMBOL: unique symbol = Symbol.for('someController'); | ||
// get the store | ||
const store = getScriptStore<SomeController>(GLOBAL_SYMBOL); | ||
// get the controller | ||
const controller = useController<SomeController>(store); | ||
``` | ||
See example in [diplodoc-platform/html-extension](https://github.com/diplodoc-platform/html-extension/blob/main/src/react/useDiplodocHtml.ts) |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15359
7
357
58
1
14
1