Skip to content

LDflex/async-iteration-handlers

Repository files navigation

@ldflex/async-iteration-handlers

This library acts as a wrapper for functions from the async library, exporting them as handlers to be used by LDflex.

npm version build Dependency Status

All exported handlers (defaultIterationHandlers)

Initialization

const { PathFactory, defaultHandlers } = require('ldflex');
const { default: ComunicaEngine } = require('@ldflex/comunica');
const { default: defaultIterationHandlers } = require('@ldflex/async-iteration-handlers');
const { namedNode } = require('@rdfjs/data-model');

// The JSON-LD context for resolving properties
const context = {
  "@context": {
    "@vocab": "http://xmlns.com/foaf/0.1/",
    "friends": "knows",
    "label": "http://www.w3.org/2000/01/rdf-schema#label",
  }
};
// The query engine and its source
const queryEngine = new ComunicaEngine('https://ruben.verborgh.org/profile/');
// The object that can create new paths
const path = new PathFactory({
  context,
  queryEngine,
  handlers: {
    ...defaultHandlers,
    ...defaultIterationHandlers
  }
});

const ruben = path.create({ subject: namedNode('https://ruben.verborgh.org/profile/#me') });

ruben.friends.forEach(async (x: any) => {
  console.log(`${await x}`)
});

Available Methods

  • .every
  • .everyLimit
  • .everySeries
// This executes asynchronously
const allFriendsLabelled: Promise<boolean> = path.friends.every(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes synchronously
const allFriendsLabelled: Promise<boolean> = path.friends.everySeries(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
const allFriendsLabelled: Promise<boolean> = path.friends.everyLimit(
  async friend => `${await friend.label}` !== 'undefined',
  5
);
  • .filter
  • .filterLimit
  • .filterSeries
// This executes asynchronously
const labelledFriends: Promise<string[]> = path.friends.filter(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes synchronously
const labelledFriends: Promise<string[]> = path.friends.filterSeries(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
const labelledFriends: Promise<string[]> = path.friends.filterLimit(
  async friend => `${await friend.label}` !== 'undefined',
  5
);
  • .find
  • .findLimit
  • .findSeries
// This executes asynchronously
const labelledFriend: Promise<string> = path.friends.find(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes synchronously
const labelledFriend: Promise<string> = path.friends.findSeries(
  async friend => `${await friend.label}` !== 'undefined'
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
const labelledFriend: Promise<string> = path.friends.findLimit(
  async friend => `${await friend.label}` !== 'undefined',
  5
);
  • .forEach
  • .forEachLimit
  • .forEachSeries
const labelledFriend = [];

// This executes asynchronously
path.friends.forEach(
  async friend => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);

// This executes synchronously
path.friends.forEachSeries(
  async friend => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
path.friends.forEachLimit(
  async friend => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  },
  5
);
  • .forEachOf
  • .forEachOfLimit
  • .forEachOfSeries
const labelledFriend = [];

// This executes asynchronously
path.friends.forEachOf(
  async (friend, index) => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);

// This executes synchronously
path.friends.forEachOfSeries(
  async (friend, index) => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
path.friends.forEachOfLimit(
  async (friend, index) => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  },
  5
);
  • .map
  • .mapLimit
  • .mapSeries
// This executes asynchronously
const friendLabels: Promise<string[]> = path.friends.map(
  async friend => `${await friend.label}`
);

// This executes synchronously
const friendLabels: Promise<string[]> = path.friends.mapSeries(
  async friend => `${await friend.label}`
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
const friendLabels: Promise<string[]> = path.friends.mapLimit(
  async friend => `${await friend.label}`,
  5
);
  • .some
  • .someLimit
  • .someSeries
// This executes asynchronously
const someFriendsLabelled: Promise<boolean> = path.friends.some(
  friend => `${friend.label}` !== 'undefined'
);

// This executes synchronously
const someFriendsLabelled: Promise<boolean> = path.friends.someLimit(
  friend => `${friend.label}` !== 'undefined'
);

// This executes asynchronously, but at most 5 entities are processed in parrallel at any time
const someFriendsLabelled: Promise<boolean> = path.friends.someSeries(
  friend => `${friend.label}` !== 'undefined',
  5
);
  • .reduce
// This executes synchronously
const friendLabels: Promise<string>= path.friends.reduce(
  async (total, friend) => `${total}&${await friend.label}`,
  ''
);

Alternatively if you wish to only use sychronous handlers (seriesIterationHandlers)

Initialization

const { PathFactory } = require('ldflex');
const { default: ComunicaEngine, defaultHandlers } = require('@ldflex/comunica');
const { seriesIterationHandlers } = require('@ldflex/async-iteration-handlers');
const { namedNode } = require('@rdfjs/data-model');

// The JSON-LD context for resolving properties
const context = {
  "@context": {
    "@vocab": "http://xmlns.com/foaf/0.1/",
    "friends": "knows",
    "label": "http://www.w3.org/2000/01/rdf-schema#label",
  }
};
// The query engine and its source
const queryEngine = new ComunicaEngine('https://ruben.verborgh.org/profile/');
// The object that can create new paths
const path = new PathFactory({
  context,
  queryEngine,
  handlers: [
    ...defaultHandlers,
    ...seriesIterationHandlers
  ]
});

Available Methods

  • .every
// This executes synchronously
const allFriendsLabelled: Promise<boolean> = path.friends.every(
  async friend => `${await friend.label}` !== 'undefined'
);
  • .filter
// This executes synchronously
const labelledFriends: Promise<string[]> = path.friends.filter(
  async friend => `${await friend.label}` !== 'undefined'
);
  • .find
// This executes synchronously
const labelledFriend: Promise<string> = path.friends.find(
  async friend => `${await friend.label}` !== 'undefined'
);
  • .forEach
const labelledFriend = [];

// This executes synchronously
path.friends.forEach(
  async friend => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);
  • .forEachOf
const labelledFriend = [];

// This executes synchronously
path.friends.forEachOf(
  async (friend, index) => {
    if (`${await friend.label}` !== 'undefined') {
      labelledFriend.push(friend)
    }
  }
);
  • .map
// This executes synchronously
const friendLabels: Promise<string[]> = path.friends.map(
  async friend => `${await friend.label}`
);
  • .some
// This executes synchronously
const someFriendsLabelled: Promise<boolean> = path.friends.some(
  friend => `${friend.label}` !== 'undefined'
);
  • .reduce
// This executes synchronously
const friendLabels: Promise<string>= path.friends.reduce(
  async (total, friend) => `${total}&${await friend.label}`,
  ''
);

NOTE

By default, methods with limited asynchronicity process at most 5 entities concurrently.

License

©2020–present Jesse Wright, Ruben Verborgh, Ruben Taelman. MIT License.

About

LDflex handlers for asynchronous iteration operations

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published