🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

causalityjs

Package Overview
Dependencies
Maintainers
2
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

causalityjs - npm Package Compare versions

Comparing version

to
3.1.1

53

causality.js

@@ -19,6 +19,6 @@ import { argumentsToArray, configSignature, mergeInto } from "./lib/utility.js";

warnOnNestedRepeater: true,
alwaysDependOnParentRepeater: false,
objectMetaProperty: "causality",
emitEvents: false, // either set onEventGlobal or define onChange on individual objects.
sendEventsToObjects: true,

@@ -86,3 +86,3 @@ // Reserved properties that you can override on observables IF sendEventsToObjects is set to true.

const instance = {
const world = {
// Main API

@@ -139,5 +139,5 @@ observable,

const dependencyInterface = configuration.customDependencyInterfaceCreator ?
configuration.customDependencyInterfaceCreator(instance)
configuration.customDependencyInterfaceCreator(world)
:
defaultDependencyInterfaceCreator(instance);
defaultDependencyInterfaceCreator(world);
const recordDependencyOnArray = dependencyInterface.recordDependencyOnArray;

@@ -154,3 +154,3 @@ const recordDependencyOnEnumeration = dependencyInterface.recordDependencyOnEnumeration;

// Object.assign(instance, require("./lib/causalityObject.js").bindToInstance(instance));
// Object.assign(world, require("./lib/causalityObject.js").bindToInstance(world));

@@ -179,3 +179,2 @@

objectMetaProperty,
emitEvents,
sendEventsToObjects,

@@ -189,2 +188,3 @@ onEventGlobal,

const emitEvents = !!onEventGlobal || sendEventsToObjects;

@@ -366,3 +366,3 @@ /**********************************

if (onReadGlobal && !onReadGlobal(this, target)) {
if (onReadGlobal && !onReadGlobal(this, target, key)) {
return cannotReadPropertyValue;

@@ -387,3 +387,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -439,3 +439,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -481,3 +481,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) {
if (onReadGlobal && !onReadGlobal(this, target, key)) {
return cannotReadPropertyValue;

@@ -497,3 +497,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -513,3 +513,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) {
if (onReadGlobal && !onReadGlobal(this, target, key)) {
return cannotReadPropertyValue;

@@ -541,3 +541,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) { //Used for ensureInitialized, registerActivity & canRead
if (onReadGlobal && !onReadGlobal(this, target, key)) { //Used for ensureInitialized, registerActivity & canRead
return cannotReadPropertyValue;

@@ -570,3 +570,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -610,3 +610,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -637,3 +637,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) { //Used for ensureInitialized, registerActivity & canRead
if (onReadGlobal && !onReadGlobal(this, target, key)) { //Used for ensureInitialized, registerActivity & canRead
return cannotReadPropertyValue;

@@ -656,3 +656,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) { //Used for ensureInitialized, registerActivity & canRead
if (onReadGlobal && !onReadGlobal(this, target, key)) { //Used for ensureInitialized, registerActivity & canRead
return cannotReadPropertyValue;

@@ -672,3 +672,3 @@ }

if (onWriteGlobal && !onWriteGlobal(this, target)) {
if (onWriteGlobal && !onWriteGlobal(this, target, key)) {
return;

@@ -688,3 +688,3 @@ }

if (onReadGlobal && !onReadGlobal(this, target)) { //Used for ensureInitialized, registerActivity & canRead
if (onReadGlobal && !onReadGlobal(this, target, key)) { //Used for ensureInitialized, registerActivity & canRead
return cannotReadPropertyValue;

@@ -754,2 +754,3 @@ }

handler.meta = {
world: world,
id: state.nextObjectId++,

@@ -1164,3 +1165,3 @@ buildId : buildId,

const repeater = createRepeater(description, repeaterAction, repeaterNonRecordingAction, options, finishRebuilding);
if (options.dependentOnParent && state.context.type === "repeater") {
if (state.context && state.context.type === "repeater" && (options.dependentOnParent || configuration.alwaysDependOnParentRepeater)) {
state.context.addChild(repeater);

@@ -1243,10 +1244,10 @@ }

*
* Return instance
* Return world
*
************************************************************************/
return instance;
return world;
}
let instances = {};
let worlds = {};

@@ -1258,8 +1259,8 @@ export function getWorld(configuration) {

if (typeof(instances[signature]) === 'undefined') {
instances[signature] = createWorld(configuration);
if (typeof(worlds[signature]) === 'undefined') {
worlds[signature] = createWorld(configuration);
}
return instances[signature];
return worlds[signature];
}
export default getWorld;

@@ -0,16 +1,46 @@

const colorCodes = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
}
// Configuration
let configuration = {
// Count the number of chars that can fit horizontally in your buffer. Set to -1 for one line logging only.
bufferWidth : 83,
// bufferWidth : 83
// bufferWidth : 76
indentToken : " ",
// Change to true in order to find all logs hidden in your code.
findLogs : false,
// Set to true in web browser that already has a good way to display objects with expandable trees.
useConsoleDefault : false
};
// Count the number of chars that can fit horizontally in your buffer. Set to -1 for one line logging only.
bufferWidth : 83,
// bufferWidth : 83
// bufferWidth : 76
indentToken : " ",
// Change to true in order to find all logs hidden in your code.
findLogs : false,
// Set to true in web browser that already has a good way to display objects with expandable trees.
useConsoleDefault : false
};

@@ -20,266 +50,312 @@ // State

function stacktrace() {
function st2(f) {
return !f ? [] :
st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
}
return st2(arguments.callee.caller);
}
function indentString(level) {
let string = "";
while (level-- > 0) {
string = string + configuration.indentToken;
}
return string;
let string = "";
while (level-- > 0) {
string = string + configuration.indentToken;
}
return string;
}
function createBasicContext() {
return {
terminated : false,
rootLevel : true,
horizontal : false,
indentLevel : globalIndentLevel,
unfinishedLine : false
}
const context = {
terminated : false,
rootLevel : true,
horizontal : false,
indentLevel : globalIndentLevel,
unfinishedLine : false
};
context.resetColor = () => {
context.setColor("Reset");
}
return context;
}
function createToStringContext() {
let context = createBasicContext();
context.result = "";
context.log = function(string) {
if (this.unfinishedLine) {
this.result += string;
this.unfinishedLine = true;
} else {
this.result += indentString(this.indentLevel) + string;
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {
if (this.unfinishedLine && !this.horizontal) {
this.result += "\n";
this.unfinishedLine = false;
}
};
return context;
let context = createBasicContext();
context.result = "";
context.log = function(string) {
if (this.unfinishedLine) {
this.result += string;
this.unfinishedLine = true;
} else {
this.result += indentString(this.indentLevel) + string;
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {
if (this.unfinishedLine && !this.horizontal) {
this.result += "\n";
this.unfinishedLine = false;
}
};
context.setColor = function() {}
context.jsonCompatible = true;
return context;
}
function createStdoutContext() {
let context = createBasicContext();
context.log = function(string) {
if (this.unfinishedLine) {
process.stdout.write(string);
this.unfinishedLine = true;
} else {
let indent = indentString(this.indentLevel);
process.stdout.write(indent + string);
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {
if (this.unfinishedLine && !this.horizontal) {
console.log();
this.unfinishedLine = false;
}
};
return context;
let context = createBasicContext();
context.log = function(string) {
if (this.unfinishedLine) {
process.stdout.write(string);
this.unfinishedLine = true;
} else {
let indent = indentString(this.indentLevel);
process.stdout.write(indent + string);
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {
if (this.unfinishedLine && !this.horizontal) {
console.log();
this.unfinishedLine = false;
}
};
context.setColor = function(colorCode) {
if (!colorCodes[colorCode]) colorCode = "Reset";
context.log(colorCodes[colorCode]);
}
context.jsonCompatible = false;
return context;
}
function createHorizontalMeasureContext(limit) {
let context = createBasicContext();
context.horizontal = true;
context.count = 0;
context.limit = limit;
context.log = function(string) {
if (this.unfinishedLine) {
this.count += string.length;
this.terminated = this.count > this.limit;
this.unfinishedLine = true;
} else {
let indent = indentString(this.indentLevel);
this.count += (indent + string).length;
this.terminated = this.count > this.limit;
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {};
return context;
function createHorizontalMeasureContext(limit, parent) {
let context = createBasicContext();
context.horizontal = true;
context.count = 0;
context.limit = limit;
context.log = function(string) {
if (this.unfinishedLine) {
this.count += string.length;
this.terminated = this.count > this.limit;
this.unfinishedLine = true;
} else {
let indent = indentString(this.indentLevel);
this.count += (indent + string).length;
this.terminated = this.count > this.limit;
this.unfinishedLine = true;
}
};
context.finishOpenLine = function() {};
context.setColor = function() {};
context.jsonCompatible = parent.jsonCompatible;
return context;
}
function horizontalLogFitsWithinWidthLimit(entity, pattern, limit) {
let context = createHorizontalMeasureContext(limit);
logPattern(entity, pattern, context);
return !context.terminated;
function horizontalLogFitsWithinWidthLimit(entity, pattern, limit, parentContext) {
let context = createHorizontalMeasureContext(limit, parentContext);
logPattern(entity, pattern, context);
return !context.terminated;
}
function logPattern(entity, pattern, context) {
if (typeof(pattern) === "undefined") {
pattern = 1;
}
// Apply transformation
// let originalEntity = entity;
if (typeof(pattern) === 'function') {
// console.log(pattern);
// console.log(entity);
entity = pattern(entity);
pattern = -1;
}
// Setup of process
let outer = false;
if (typeof(context) === 'undefined') {
context = createStdoutContext();
outer = true;
}
// Bail out if terminated
if (context.terminated) return;
const rootLevel = context.rootLevel;
const json = context.jsonCompatible;
context.rootLevel = false;
// Recursive rendering
if (typeof(entity) !== 'object') {
if (typeof(entity) === 'function') {
context.log("function( ... ) { ... }");
} else if (typeof(entity) === 'string') {
if (context.rootLevel) {
context.log(entity);
} else {
context.log('"' + entity + '"');
}
} else {
context.log(entity + "");
}
} else if (entity === null) {
context.log("null");
} else {
if (pattern === 0) {
if (entity instanceof Array) {
context.log("[...]");
} else {
context.log("{...}");
}
} else {
let isArray = (entity instanceof Array);
let startedHorizontal = false;
if (!context.horizontal) {
let spaceLeft = configuration.bufferWidth - (context.indentLevel * configuration.indentToken.length);
context.horizontal = configuration.bufferWidth === -1 ? true : horizontalLogFitsWithinWidthLimit(entity, pattern, spaceLeft);
startedHorizontal = context.horizontal;
}
if (isArray) context.finishOpenLine(); // Should not be when enforced single row.
context.log(isArray ? "[" : "{");
// context.log(context.horizontal ? "-" : "|");
context.finishOpenLine();
context.indentLevel++;
let first = true;
for (p in entity) {
if (!first) {
context.log(", ");
context.finishOpenLine();
}
if (!isArray || isNaN(p)) context.log(p + " : ");
let nextPattern = null;
if (typeof(pattern) === 'object') {
nextPattern = pattern[p];
} else {
nextPattern = pattern === -1 ? -1 : pattern - 1;
}
if(!isArray) context.indentLevel++;
context.rootLevel = false;
logPattern(entity[p], nextPattern, context);
if(!isArray) context.indentLevel--;
first = false;
}
context.indentLevel--;
context.finishOpenLine();
context.log(isArray ? "]" : "}");
if (startedHorizontal) {
context.horizontal = false;
}
}
}
if (outer) context.finishOpenLine();
if (typeof(pattern) === "undefined") {
pattern = 1;
}
// Apply transformation
// let originalEntity = entity;
if (typeof(pattern) === 'function') {
// console.log(pattern);
// console.log(entity);
entity = pattern(entity);
pattern = -1;
}
// Bail out if terminated
if (context.terminated) return;
// Recursive rendering
if (typeof(entity) !== 'object') {
if (typeof(entity) === 'function') {
context.setColor("FgBlue");
context.log("function( ... ) { ... }");
context.resetColor();
} else if (typeof(entity) === 'string') {
if (rootLevel) {
context.log(entity);
} else {
context.setColor("FgGreen");
const quote = json ? '"' : "'";
context.log(quote + entity + quote);
context.resetColor();
}
} else {
context.setColor("FgYellow");
context.log(entity + "");
context.resetColor();
}
} else if (entity === null) {
context.log("null");
} else {
if (pattern === 0) {
if (entity instanceof Array) {
context.log("[");
context.setColor("FgCyan");
context.log("...");
context.resetColor();
context.log("]");
} else {
context.log("{");
context.setColor("FgCyan");
context.log("...");
context.resetColor();
context.log("}");
}
} else {
let isArray = (entity instanceof Array);
const keyCount = Object.keys(entity).length;
let startedHorizontal = false;
if (!context.horizontal) {
let spaceLeft = configuration.bufferWidth - (context.indentLevel * configuration.indentToken.length);
context.horizontal = configuration.bufferWidth === -1 ? true : horizontalLogFitsWithinWidthLimit(entity, pattern, spaceLeft, context);
startedHorizontal = context.horizontal;
}
if (isArray) context.finishOpenLine(); // Should not be when enforced single row.
context.log(isArray ? "[" : "{");
if (context.horizontal && keyCount) context.log(" ");
// context.log(context.horizontal ? "-" : "|");
context.finishOpenLine();
context.indentLevel++;
let first = true;
for (let p in entity) {
if (!first) {
context.log(", ");
context.finishOpenLine();
}
if (!isArray || isNaN(p)) {
// context.setColor("FgCyan");
if (json) context.log('"');
context.log(p);
if (json) context.log('"');
context.log(': ');
// context.resetColor();
}
let nextPattern = null;
if (typeof(pattern) === 'object') {
nextPattern = pattern[p];
} else {
nextPattern = pattern === -1 ? -1 : pattern - 1;
}
if(!isArray) context.indentLevel++;
logPattern(entity[p], nextPattern, context);
if(!isArray) context.indentLevel--;
first = false;
}
context.indentLevel--;
context.finishOpenLine();
if (context.horizontal && keyCount) context.log(" ");
context.log(isArray ? "]" : "}");
if (startedHorizontal) {
context.horizontal = false;
}
}
}
if (rootLevel) context.finishOpenLine();
}
export const objectlog = {
// Configuration
configuration : configuration,
log(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.log(entity);
} else {
logPattern(entity, pattern);
}
},
// Configuration
configuration,
stacktrace,
log(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.log(entity);
} else {
logPattern(entity, pattern, createStdoutContext());
}
},
// If you need the output as a string.
logToString(entity, pattern) {
let context = createToStringContext();
logPattern(entity, pattern, context);
return context.result;
},
// If you need the output as a string.
logToString(entity, pattern) {
let context = createToStringContext();
logPattern(entity, pattern, context);
return context.result;
},
loge(string) { // "event"
this.log("<<<" + string + ">>>");
},
loge(string) { // "event"
this.log("<<<" + string + ">>>");
},
logs() { // "separator"
this.log("---------------------------------------")
},
logs() { // "separator"
this.log("---------------------------------------")
},
logss() {
this.log("=======================================")
},
logss() {
this.log("=======================================")
},
logsss() {
this.log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
},
logVar(name, entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.log(name + ":");
console.group();
console.log(entity);
console.groupEnd();
} else {
context = createStdoutContext();
if (typeof(pattern) === 'undefined') pattern = 1;
context.log(name + ": ");
let spaceLeft = configuration.bufferWidth - (context.indentLevel * configuration.indentToken.length) - (name + ": ").length;
context.horizontal = configuration.bufferWidth === -1 ? true : horizontalLogFitsWithinWidthLimit(entity, pattern, spaceLeft);
if (context.horizontal) {
logPattern(entity, pattern, context);
} else {
context.indentLevel++;
logPattern(entity, pattern, context);
context.indentLevel--;
}
}
},
group(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.group(entity);
} else {
if (typeof(entity) !== 'undefined') {
logPattern(entity, pattern);
}
globalIndentLevel++;
}
},
groupEnd(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.groupEnd();
} else {
globalIndentLevel--;
if (globalIndentLevel < 0) globalIndentLevel = 0;
if (typeof(entity) !== 'undefined') {
logPattern(entity, pattern);
}
}
}
logsss() {
this.log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
},
logVar(name, entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.log(name + ":");
console.group();
console.log(entity);
console.groupEnd();
} else {
context = createStdoutContext();
if (typeof(pattern) === 'undefined') pattern = 1;
context.log(name + ": ");
let spaceLeft = configuration.bufferWidth - (context.indentLevel * configuration.indentToken.length) - (name + ": ").length;
context.horizontal = configuration.bufferWidth === -1 ? true : horizontalLogFitsWithinWidthLimit(entity, pattern, spaceLeft);
if (context.horizontal) {
logPattern(entity, pattern, context);
} else {
context.indentLevel++;
logPattern(entity, pattern, context);
context.indentLevel--;
}
}
},
group(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.group(entity);
} else {
if (typeof(entity) !== 'undefined') {
logPattern(entity, pattern, createStdoutContext());
}
globalIndentLevel++;
}
},
groupEnd(entity, pattern) {
if (objectlog.findLogs) throw new Error("No logs allowed!");
if (configuration.useConsoleDefault) {
console.groupEnd();
} else {
globalIndentLevel--;
if (globalIndentLevel < 0) globalIndentLevel = 0;
if (typeof(entity) !== 'undefined') {
logPattern(entity, pattern, createStdoutContext());
}
}
}
}
{
"name": "causalityjs",
"version": "3.0.5",
"version": "3.1.1",
"description": "A library for reactive programming based on Javascript proxies.",

@@ -5,0 +5,0 @@ "main": "causality.js",

@@ -163,5 +163,5 @@

Somtimes you just need to observe objects, and record events. For this purpose you can activate emitEvents in the causality configuration. If this is set to true, then causality will try to send events both globally, to an onEventGlobal callback that you can setup in the causality configuration.
Somtimes you just need to observe objects, and record events. For this purpose you can set an onEventGlobal callback in the causality configuration. This callback can then monitor any change that happens in the world from a birds eye perspective.
Activating emitEvents in conjunction with sendEventsToObjects will also cause causality to try to send events to your observable objects as well.
Activating sendEventsToObjects will also cause causality to try to send events directly to your observable objects. If your objects implement the following callbacks, causality will then try to call them.

@@ -172,3 +172,3 @@ - onChange

onChange will send a message to the object, containing information about what happened.
onChange will receive a message, containing information about any change that happened to the object.

@@ -218,3 +218,3 @@ The onBuildCreate and onBuildRemove events will be sent specifically when doing data structure rebuilding. They correspond to the React concepts of componentDidMount and componentWillUnmount but for a generalized data structure re building framework.

`obj.observe(listener)` has been replaced with a single `obj.onChange` listener. Enable events with config `{emitEvents:true, sendEventsToObjects: true}`. It is also possible to globaly observe events by setting onEventGlobal in the configuration.
`obj.observe(listener)` has been replaced with a single `obj.onChange` callback. Enable events with config `{sendEventsToObjects: true}`. It is also possible to globaly observe events by setting onEventGlobal in the configuration.

@@ -221,0 +221,0 @@ You can also intercept reading and writing to any object in the world by setting onReadGlobal and onWriteGlobal in the configuration. That can be useful for security features.

'use strict';
require = require("esm")(module);
const {observable, repeat,trace} = require("../causality.js").getWorld({name: "array-splices", emitEvents: true});
const {observable, repeat,trace} = require("../causality.js").getWorld({name: "array-splices", sendEventsToObjects: true});
const assert = require('assert');

@@ -5,0 +5,0 @@ //const log = console.log.bind(console);

@@ -9,4 +9,3 @@ 'use strict';

const instance = causality.getWorld({
name: "rebuild",
emitEvents: true,
name: "rebuild",
onEventGlobal: (event) => {

@@ -13,0 +12,0 @@ events.push(event);